#include "os_task.h"
#include "cmpnt_mng.h"
#include "bsp_init.h"
#include "test_suit.h"

/* LED 任务 */
#define LED_THD_NAME             "led_thd"
#define LED_THD_STACK_SIZE       512
#define LED_THD_PRIORITY         25
#define LED_THD_TICK             20

static rt_thread_t led_thd = RT_NULL;
static void led_thd_entry(void *parameter)
{
    while(1)
    {
        dev_led_control(DEV_LED_ON);
        rt_thread_mdelay(50);
        dev_led_control(DEV_LED_OFF);
        rt_thread_mdelay(50);
    }
}

/* GUI tick 任务 (心跳 - tick 提供时钟, LVGL_TICK) */
#define GUITICK_THD_NAME             "guitick_thd"
#define GUITICK_THD_STACK_SIZE       1024
#define GUITICK_THD_PRIORITY         8
#define GUITICK_THD_TICK             20

static rt_thread_t guitick_thd = RT_NULL;
static void guitick_thd_entry(void *parameter)
{
    while(1)
    {
        lv_tick_inc(1);
        rt_thread_delay(GUI_INC_TICK_MS_CFG);
    }
}

/* GUI task 任务 (心跳 - 运行所有lvgl的task) */
#define GUITASK_THD_NAME             "guitask_thd"
#define GUITASK_THD_STACK_SIZE       4096
#define GUITASK_THD_PRIORITY         (GUITICK_THD_PRIORITY + 1) /* lv_tick_inc 的优先级必须高于 lv_task_handler */
#define GUITASK_THD_TICK             20

static rt_thread_t guitask_thd = RT_NULL;
static void guitask_thd_entry(void *parameter)
{   
    /* GUI主界面 */
    if (gui_user_mainwindow() != RET_OK)
    {
        while(1)
        {
            rt_thread_delay(GUI_TASK_TICK_MS_CFG);
        }
    }

    while(1)
    {
        lv_task_handler();
        rt_thread_delay(GUI_TASK_TICK_MS_CFG);
    }
}

/* test 任务 (please place this thread at last!!!) */
#define TEST_THD_NAME             "test_thd"
#define TEST_THD_STACK_SIZE       1024
#define TEST_THD_PRIORITY         22
#define TEST_THD_TICK             20

static rt_thread_t test_thd = RT_NULL;
static void test_thd_entry(void *parameter)
{
    uint32_t DELAY_FOR_TEST_PERIOD_MS = 10;  /* <------ 设置测试间隔周期(ms) */

    while(1)
    {
        rt_thread_delay(DELAY_FOR_TEST_PERIOD_MS);

#if 1   /* set as 1 to enable test, set as 0 to disable test */
        
        /* use parameters to select the target and test items */
        test_target_run(TEST_TARGET_APP_AXIS, TEST_APP_AXIS);
        
#endif
    }
}

void task_init(void)
{
    /* 创建用户任务 */
    led_thd = rt_thread_create(LED_THD_NAME,
                               led_thd_entry,
                               RT_NULL,
                               LED_THD_STACK_SIZE,
                               LED_THD_PRIORITY,
                               LED_THD_TICK);

    if (led_thd != NULL)
    {
        rt_thread_startup(led_thd);
    }

    /* GUI tick 任务 */
    guitick_thd = rt_thread_create(GUITICK_THD_NAME,
                                   guitick_thd_entry,
                                   RT_NULL,
                                   GUITICK_THD_STACK_SIZE,
                                   GUITICK_THD_PRIORITY,
                                   GUITICK_THD_TICK);

    if (guitick_thd != NULL)
    {
        rt_thread_startup(guitick_thd);
    }

    /* GUI task 任务 */
    guitask_thd = rt_thread_create(GUITASK_THD_NAME,
                                   guitask_thd_entry,
                                   RT_NULL,
                                   GUITASK_THD_STACK_SIZE,
                                   GUITASK_THD_PRIORITY,
                                   GUITASK_THD_TICK);

    if (guitask_thd != NULL)
    {
        rt_thread_startup(guitask_thd);
    }

    /* TEST task 任务 (please place this thread at last!!!) */
    test_thd = rt_thread_create(TEST_THD_NAME,
                                test_thd_entry,
                                RT_NULL,
                                TEST_THD_STACK_SIZE,
                                TEST_THD_PRIORITY,
                                TEST_THD_TICK);

    if (test_thd != NULL)
    {
        rt_thread_startup(test_thd);
    }

    return;
}
